//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _APPS_BUSH_SYNTAX_CREDIRECTION_H_
#define _APPS_BUSH_SYNTAX_CREDIRECTION_H_

#include <buola/buola.h>
#include <buola/io/cfdstream.h>
#include "parser/cword.h"
#include "parser/ctoken.h"
#include "parser/cparsedword.h"

namespace buola { namespace bush {

struct CRedirection : public UShared
{
    CRedirection(const CToken &pFD,const CToken &pOp,const CParsedWord &pWord)
        :   mParsedWord(pWord)
        ,   mDstFD(-1)
        ,   mSrcFD(-1)
        ,   mOp(pOp.mToken)
    {
        if(pFD.mToken==TOKEN_INTEGER)
        {
            mDstFD=to_<int>(std::wstring(pFD.mB,pFD.mE));
        }
    }
    
    CRedirection(int pDstFD,int pSrcFD,EToken pOp)
        :   mDstFD(pDstFD)
        ,   mSrcFD(pSrcFD)
        ,   mOp(pOp)
    {
        
    }
    
    void Print(int pDepth=0)
    {
        for(int i=0;i<pDepth;++i)
            msg_info() << "   ";
        msg_info() << mDstFD << ' ' << (int)mOp << ' ' << mWord.mChars << "\n";
    }

    void Apply(CProcess &pProcess);

    io::PFDStream GetOutputStream();
    
    CParsedWord mParsedWord;
    CWord mWord;
    int mDstFD;
    int mSrcFD;
    EToken mOp;
};

typedef intrusive_ptr<CRedirection> PRedirection;

/*namespace bush*/ } /*namespace buola*/ }

#endif
